import {NTime, NTag, NImage, FormItemRule, DataTableColumns} from "naive-ui";
import {getImageUrl} from "@/utils/wish";

export const columns: DataTableColumns<RowData> = [
    {
        title: '图片',
        key: 'url',
        align: 'center',
        render: (row: any) => {
            return h(NImage, {
                src: getImageUrl(row.url),
                width: 50
            });
        }
    },
    {
        title: '排序',
        key: 'sort',
        render: (row: any) => {
            return h(NTag, {
                type:"warning"
            }, {
                default: () => row.sort
            })
        }
    },
    {
        title: '创建时间',
        key: 'createdAt',
        ellipsis: {
            tooltip: true
        },
        render: (row: any) => {
            return h(NTime, {
                time: new Date(row.createdAt)
            })
        }
    },
    {
        title: '创建者',
        key: 'createName',
    },
    {
        title: '最后修改时间',
        key: 'updatedAt',
        ellipsis: {
            tooltip: true
        },
        render: (row: any) => {
            return h(NTime, {
                time: new Date(row.updatedAt)
            })
        }
    },
]

export const formInfo = {
    url: ``,
    sort: 0,
}

export const rulesInfo = {
    url: {
        required: true,
        validator (rule: FormItemRule, value: string) {
            if (!formInfo.url) {
                return new Error('请上传轮播图')
            }
            return true
        },
    },
    sort: {
        required: true,
        validator (rule: FormItemRule, value: string) {
            if (!value) {
                return new Error('排序为必填项')
            }
            return true
        },
        trigger: ['input', 'blur']
    }
}
